/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/12/12 11:17
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.service;

import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.enums.Constant;
import com.spring.arch.uaa.enums.OrganizationType;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.model.Organization;
import com.spring.arch.uaa.model.Tenant;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.enums.Constant;
import com.spring.arch.uaa.enums.OrganizationType;
import com.spring.arch.uaa.model.Organization;
import com.spring.arch.uaa.model.Tenant;
import com.spring.arch.uaa.repository.TenantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

import static com.spring.arch.uaa.errorcode.UaaErrorCode.*;

/**
 * 租户服务层接口
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2019/12/12 11:17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TenantService {

    @Autowired
    private TenantRepository tenantRepository;
    @Autowired
    private OrganizationService organizationService;

    /**
     * 创建租户
     * @param model 租户model
     * @return 租户信息
     */
    public Tenant save(Tenant model) {
        // 判断租户名称、简称唯一
        boolean existsName;
        boolean existsAbbreviation;
        if (model.isNew()) {
            existsName = tenantRepository.existsByName(model.getName());
            existsAbbreviation = tenantRepository.existsByAbbreviation(model.getAbbreviation());
        } else {
            existsName = tenantRepository.existsByNameAndIdNot(model.getName(), model.getId());
            existsAbbreviation = tenantRepository.existsByAbbreviationAndIdNot(model.getAbbreviation(), model.getId());
        }
        if (existsName) {
            throw ExceptionHolder.serviceException(UaaErrorCode.E2001001003, model.getName());
        }
        if (existsAbbreviation) {
            throw ExceptionHolder.serviceException(UaaErrorCode.E2001001005, model.getAbbreviation());
        }

        // 创建租户
        Tenant tenant = tenantRepository.save(model);
        // 创建根组织
        Organization rootOrg = saveTenantRootOrganization(tenant);
        tenant.setRootOrganization(rootOrg);
        return tenantRepository.save(tenant);
    }

    /**
     * 创建租户根组织节点
     * @param tenant 租户
     * @return 组织
     */
    public Organization saveTenantRootOrganization(Tenant tenant) {
        Organization rootOrg;
        if (tenant.isNew() || tenant.getRootOrganization() == null) {
            rootOrg = new Organization();
        } else {
            rootOrg = tenant.getRootOrganization();
        }
        rootOrg.setTenant(tenant);
        rootOrg.setParent(null);
        rootOrg.setProject(null);
        rootOrg.setCode(Constant.tenantRootOrgCode(tenant.getCode()));
        rootOrg.setName(tenant.getName());
        rootOrg.setType(OrganizationType.ORGANIZATION);
        return organizationService.save(rootOrg);
    }

    public void delete(UUID id) {
        final Tenant tenant = tenantRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001001001, String.valueOf(id)));
        tenant.setDisable(true);
        tenantRepository.save(tenant);
    }
}
